<template>
  <div>
    <!--面包屑-->
    <bread :bread="$route.meta.bread" />
    <!--页面-->
    <el-card>
      <!--搜索框-->
      <el-button type="primary" @click="dialogFormVisible = true">添加分类</el-button>
      <!--表格-->
      <el-table
        :data="tableData"
        style="width: 100%"
        border
        lazy
        row-key="cat_id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="#" width="80" type="index">
          <template slot-scope="scope">
            <span v-show="scope.row.cat_level == 0">{{ 1 }}</span>
            <span v-show="scope.row.cat_level != 0"></span>
          </template>
        </el-table-column>
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column label="是否有效">
          <template slot-scope="scope">
            <i
              class="el-icon-success"
              style="color: green"
              v-show="scope.row.cat_deleted == false"
            ></i>
          </template>
        </el-table-column>
        <el-table-column label="排序">
          <template slot-scope="scope">
            <el-tag v-show="scope.row.cat_level == '0'">一级</el-tag>
            <el-tag type="success" v-show="scope.row.cat_level == '1'">二级</el-tag>
            <el-tag type="warning" v-show="scope.row.cat_level == '2'">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="flag = true"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" @click="del(scope.row.cat_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!--添加分类-->
    <el-dialog title="添加分类" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="详细地址" label-width="100px" prop="addresses">
          <el-input v-model="form.addresses" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" label-width="100px" prop="address">
          <div class="block">
            <el-cascader :options="options" @change="handleChange"></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <!--编辑-->
    <el-dialog title="分类名称" :visible.sync="flag">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="详细地址" label-width="100px" prop="addresses">
          <el-input v-model="form.addresses" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag = false">取 消</el-button>
        <el-button type="primary" @click="flag = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import bread from "./../bread";
export default {
  components: { bread },
  name: "",
  data() {
    return {
      tableData: [],
      type: [1, 2, 3],
      pagenum: 1,
      pagesize: 5,
      total: 0,
      dialogFormVisible: false,
      flag: false,
      rules: {
        addresses: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
      },
      form: {
        address: "",
        addresses: "",
      },
      options: [
        {
          value: "hebei",
          label: "河北",
          children: [
            {
              value: "shijiazhuang",
              label: "石家庄",
              children: [
                {
                  value: "changanqu",
                  label: "长安区",
                },
                {
                  value: "yuhuaqu",
                  label: "裕华区",
                },
                {
                  value: "jingxingxian",
                  label: "井陉县",
                },
                {
                  value: "luquanshi",
                  label: "鹿泉市",
                },
              ],
            },
          ],
        },
      ],
    };
  },
  mounted() {
    this.lcgetlist();
  },
  methods: {
    //获取数据
    async lcgetlist() {
      try {
        let { data: res } = await this.$axios.get(
          `categories?pagenum${this.pagenum}&pagesize${this.pagesize}`
        );
        console.log(res);
        this.tableData = res;
        this.total = res.length;
      } catch (error) {
        console.log(error);
      }
    },
    //删除
    del(id) {
      this.$confirm("此操作将永久删除该, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let { data: res } = await this.$axios.delete(`categories/${id}`);
          this.lcgetlist();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //添加分类
    addlist() {},
    //分页
    handleSizeChange(size) {
      this.pagesize = size;
    },
    handleCurrentChange(page) {
      this.pagenum = page;
    },
    //联动导航
    handleChange(value) {
      console.log(value);
    },
  },
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped>
.el-table {
  margin: 15px 0px;
}
.el-dialog {
  .el-input {
    width: 95%;
  }
  .el-cascader {
    width: 95%;
  }
}
</style>
